package ModelLayer;

import java.util.ArrayList;


/**
 * Write a description of class DVDContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<DVD> thing;
    private static DVDContainer instance=null;
    /**
     * Constructor for objects of class DVDCtr
     */
    private DVDContainer()
    {
        //set up references to containers  
        thing = new ArrayList<DVD>();
    }
    public static DVDContainer getInstance()
    {
        if(instance==null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    
    
    //Dummy implementation: more parameters are probaly to added
    public void createDVD(int id, String title, String artist, String date)
    {       
          //this method constructs a new dvd object 
          //and stores it in the container
          DVD newD = new DVD(id, title, artist, date);
          
          thing.add(newD);
    }
    public int findDVD( int no)
    {
        //this method is to search the container for a DVD
        //with an id-number equal to the parameter no
        //when the dvd is found, the method is to call print-methods in the TUI layer
        
        int b;
        int x=0;
        int a;
        a = (thing.size());
        while(x<a)
        {
            DVD p = thing.get(x);
            b=p.getID();
            if(b==no)
            {
                return x;
            }
            x++;
        }
        return 0;
   
    }
    public void deleteDVD(int no)
    {
        
        //search for the dvd and delete the object from the container
        int c=findDVD(no);
        if(c!=0)
        {
            thing.remove(thing.get(c));
        }
   
    }
    public ArrayList<Copy> listAllCopies(int id)
    {
     
        int c=findDVD(id);
        if(c!=0)
        {
            return thing.get(c).getCopies();
        }
        else
        {
        return null;
       }
    }
    public Copy checkID(int search)
    {
       
        int p = findDVD(search);
        if(p!=0)
        {
            return thing.get(p).check();
        }
        else
        {return null;
        }
    }
    public void createCopy(int id, Copy newItem)
    {
        //create a copy of the specified dvd and add it to the dvd
        int c=findDVD(id);
        
        thing.get(c).addCopy(newItem);
  
    }
    public void createCopies(int no, int serial, double price, String date)
    {
      Copy add = new Copy(serial, date, price);
      createCopy(no, add);
   
    }
    
    public ArrayList<DVD> listAllDvds()
    {
          //returns a list of all dvds and their associated copies
          return thing;
    }
    
    public String getTitle(int x)  
    {
        return thing.get(x).getTitle();
    }
    public void rentCopy(int rentDVDID, long rentCopyID, String loanDate, String loanPeriod)
    {
        int r = findDVD(rentDVDID);
        thing.get(r).rentCopy(rentCopyID, loanDate, loanPeriod);
     
    }
    public Copy noIdea(int sdvd, int specie)
    {
        int s = findDVD(sdvd);
        return thing.get(s).findCopy(specie);
    }
}
